package com.henry.chatkit.utils

import android.graphics.*
import android.media.ExifInterface
import android.net.Uri
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.henry.chatkit.ChatKit
import com.henry.chatkit.Constants
import com.henry.chatkit.conversation.ConversationInfo
import com.henry.chatkit.conversation.UserInfo
import com.henry.chatkit.glide.GlideEngine
import com.henry.chatkit.messages.MessageProperties
import java.io.File
import java.io.IOException
import java.io.InputStream

object ImageUtils {

    fun getThumbImageSize(imgWidth: Int, imgHeight: Int): List<Int> {
        return getThumbImageSize(imgWidth, imgHeight, MessageProperties.getInstance().imageMinSize, MessageProperties.getInstance().imageMaxSize)
    }

    fun getThumbImageSize(imgWidth: Int, imgHeight: Int, imageMinSize: Int, imageMaxSize: Int): List<Int> {
        var width = 0
        var height = 0
        if (imgWidth < imageMinSize || imgHeight < imageMinSize) {
            width = SizeUtils.dp2px(imageMinSize)
            height = SizeUtils.dp2px(imageMinSize)
            return listOf(width, height)
        }
        if (imgWidth > imgHeight) {
            if (imgWidth > imageMaxSize) {
                width = imageMaxSize
                height = imageMaxSize * imgHeight / imgWidth
            } else {
                width = imgWidth
                height = imgHeight
            }
        } else {
            if (imgHeight > imageMaxSize) {
                width = imageMaxSize * imgWidth / imgHeight
                height = imageMaxSize
            } else {
                width = imgWidth
                height = imgHeight
            }
        }

        width = SizeUtils.dp2px(width)
        height = SizeUtils.dp2px(height)
        return listOf(width, height)

    }

    /**
     * 存储图片到本地
     */
    fun storeBitmap(outFile: File, bitmap: Bitmap?): Boolean {
        return com.blankj.utilcode.util.ImageUtils.save(bitmap, outFile, Bitmap.CompressFormat.PNG)
    }

    fun getTextSize(string: String, textSize: Int): Rect {
        val paint = Paint()
        paint.textSize = textSize.toFloat()
        val rect = Rect()
        paint.getTextBounds(string,0,string.length,rect)
        return rect
    }

    fun getTextBitmap(string: String?, path: String): Bitmap {
        var text = "X"
        if (string != null) {
            if (string.isNotEmpty()) {
                text = string.substring(0, 1)
            }
        }

        val rect = getTextSize(text, 50)
        val x = (100 - rect.width()) / 2 - 2
        val y = (100 - rect.height()) / 3

        var bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
        bitmap = com.blankj.utilcode.util.ImageUtils.drawColor(bitmap, ColorUtils.getRandomColor(false))
        bitmap = com.blankj.utilcode.util.ImageUtils.addTextWatermark(
            bitmap, text, 50, Color.WHITE,
            x.toFloat(), y.toFloat()
        )

        if (!StringUtils.isEmpty(path)) {
            val file = File(path)
            storeBitmap(file, bitmap)
        }

        return bitmap
    }

    fun deleteConversationIconPath(id: String) {
        val path = Constants.IMAGE_BASE_DIR + EncryptUtils.getMD5String(id)
        val file = File(path)
        file.delete()
    }

    fun getConversationIconPath(conv: ConversationInfo): String {
        val path = Constants.IMAGE_BASE_DIR + EncryptUtils.getMD5String(conv.id.toString())
        val file = File(path)
        if (file.exists() && file.isFile) {
            return path
        }
        getTextBitmap(conv.title, path)
        return path
    }

    fun getConversationIconPaths(conv: ConversationInfo, userInfoList: List<UserInfo>): List<String> {
        if (userInfoList.isEmpty()) {
            return listOf(getConversationIconPath(conv))
        }
//        val path = Constants.IMAGE_BASE_DIR + EncryptUtils.getMD5String(conv.id.toString())
//        val file = File(path)
//        if (file.exists() && file.isFile) {
//            return path
//        }
        val list = mutableListOf<String>()
        for (userInfo in userInfoList) {
            val path = getUserIconPath(userInfo.userId, userInfo.userName, userInfo.userIcon)
            list.add(path)
        }
        return list
    }

    fun getConversationIcon(conv: ConversationInfo): Bitmap {
        val path = Constants.IMAGE_BASE_DIR + EncryptUtils.getMD5String(conv.id.toString())
        val file = File(path)
        if (file.exists() && file.isFile) {
            return BitmapFactory.decodeFile(file.path)
        }
        val bitmap = getTextBitmap(conv.title, path)
        return bitmap
    }

    fun getConversationIcons(conv: ConversationInfo): List<Bitmap> {
        val path = Constants.IMAGE_BASE_DIR + EncryptUtils.getMD5String(conv.id.toString())
        val file = File(path)
        if (file.exists() && file.isFile) {
            return mutableListOf(BitmapFactory.decodeFile(file.path))
        }
        val bitmap = getTextBitmap(conv.title, path)
        return mutableListOf(bitmap)
    }

    fun getUserIcon(id: String?, name: String?, url: String? = null): Bitmap {
        val path = Constants.IMAGE_HEAD_DIR + EncryptUtils.getMD5String(id.toString())
        val file = File(path)
        if (file.exists() && file.isFile) {
            return BitmapFactory.decodeFile(file.path)
        }

        return if (url != null && url.isNotEmpty()) {
            val bitmap = GlideEngine.loadBitmap(url, 100)
            storeBitmap(file, bitmap)
            bitmap
        } else {
            getTextBitmap(name, path)
        }
    }

    fun getUserIconPath(id: String?, name: String?, url: String? = null): String {
        val path = Constants.IMAGE_HEAD_DIR + EncryptUtils.getMD5String(id.toString())
        val file = File(path)
        if (file.exists() && file.isFile) {
            return file.path
        }

        return if (url != null && url.isNotEmpty()) {
            val bitmap = GlideEngine.loadBitmap(url, 100)
            storeBitmap(file, bitmap)
            path
        } else {
            getTextBitmap(name, path)
            path
        }
    }
}